package com.privatetime.web;

import java.util.Date;
import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ModelDriven;
import com.privatetime.model.Group;
import com.privatetime.model.User;

public class UserAction extends BaseAction implements ModelDriven<User> {

	private static final long serialVersionUID = -8436232660926529909L;

	private User user = new User();

	private List<Integer> checkboxList;
	private Integer groupId;
	private List<User> userList;

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Integer getGroupId() {
		return groupId;
	}

	public void setGroupId(Integer groupId) {
		this.groupId = groupId;
	}

	public List<Integer> getCheckboxList() {
		return checkboxList;
	}

	public void setCheckboxList(List<Integer> checkboxList) {
		this.checkboxList = checkboxList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String create() throws Exception {
		logger.info("New User is " + user);
		user.setRegistDate(new Date());
		Integer id = userService.createEntity(user);
		logger.info("id=" + id);
		return ActionStateConstants.SUCCESS;
	}

	@Override
	public String delete() {
		
		return null;
	}

	@Override
	public String list() {
		
		return null;
	}
	
	@SkipValidation
	public String deleteUserByGroup() throws Exception {
		user = userService.retrieveEntity(user.getId());
		Group group = groupService.retrieveEntity(groupId);
		user.getGroupSet().remove(group);
		userService.saveUserGroup(user);
		return ActionStateConstants.SUCCESS;
	}
	
	@Override
	@SkipValidation
	public String retrieve() {
		logger.info("Current User id is " + user.getId());
		user = userService.retrieveEntity(user.getId());
		logger.info("Current User is " + user);
		return ActionStateConstants.SUCCESS;
	}

	@Override
	@SkipValidation
	public String update() throws Exception {
		User updateUser = userService.retrieveEntity(user.getId());
		updateUser.setUserName(user.getUserName());
		updateUser.setPassWord(user.getPassWord());
		logger.info("updateUser:" + updateUser);
		userService.updateEntity(updateUser);
		return ActionStateConstants.SUCCESS;
	}

	@SkipValidation
	public String saveUserGroup() throws Exception {
		user = userService.retrieveEntity(user.getId());
		logger.info("checkbox id:" + checkboxList.toArray().toString());
		List<Group> groupList = groupService.findGroupList(checkboxList);
		try {
			user.getGroupSet().removeAll(groupService.findEntityAll());
			for (Group group : groupList) {
				group.getUserSet().add(user);
				user.getGroupSet().add(group);
			}
			userService.saveUserGroup(user);
		} catch (Exception e) {
			logger
					.error("saveUserGroup() happens exception: "
							+ e.getMessage());
		}
		return ActionStateConstants.SUCCESS;
	}

	@SkipValidation
	public String retrieveUserByGroup() throws Exception {
		userList = userService.retrieveUserByGroup(groupId);
		return ActionStateConstants.SUCCESS;
	}
	public User getModel() {
		return user;
	}

	@Override
	public void validate() {
		if (user.getUserName() == null || user.getUserName().equals("")) {
			addFieldError("username", "username can not null");
		}
		if (user.getPassWord() == null || user.getPassWord().equals("")) {
			addFieldError("password", "password can not null");
		}

	}

}
